package sw.EngAdroid;

import netcode.client.Client;
import netcode.helper.NetcodeHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;


public class LobbyCreateGameActivity extends Activity {
	private Button createButton;
	private EditText gameName;
	private EditText hostName;
	private EditText gameTopic;
	private Client client;
	private int selectedPlayers;
	private boolean small = false;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		
		if (dm.widthPixels < 1000) {
			setContentView(R.layout.lobby_create_game_screen_small);
			small = true;
		} else
			setContentView(R.layout.lobby_create_game_screen);
		initControls();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private void initControls() {
		final Bundle gameInfo = new Bundle();

		String[] items = new String[] { "3", "4", "5", "6" };
		ArrayAdapter ad = new ArrayAdapter(this,
				android.R.layout.simple_spinner_item, items);
		ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		final Spinner spin = (Spinner) findViewById(R.id.spinner1);
		spin.setAdapter(ad);
		spin.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {


				selectedPlayers = Integer.parseInt((String) spin.getItemAtPosition(arg2));

			}

			public void onNothingSelected(AdapterView<?> arg0) {
				selectedPlayers = 3;

			}

		});

		// TODO: Make this not blow up

		gameName = (EditText) findViewById(R.id.editText1);
		hostName = (EditText) findViewById(R.id.editText2);
		gameTopic = (EditText) findViewById(R.id.editText3);
		createButton = (Button) findViewById(R.id.button1);
		createButton.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				if (gameName.getText().toString() != ""
						&& hostName.getText().toString() != ""
						&& gameTopic.getText().toString() != "") {

//					try {
//						client = new Client(NetcodeHelper.SERVER_ADDRESS,
//								NetcodeHelper.SERVER_PORT, hostName.getText()
//										.toString(), new Game());
//					} catch (Exception e) {
//						System.out.println(e);
//					}
					gameInfo.putString("gameName", gameName.getText()
							.toString());
					gameInfo.putString("playerName", hostName.getText()
							.toString());
					gameInfo.putString("topic", gameTopic.getText()
							.toString());
					gameInfo.putBoolean("isHost", true);
					
					//client.start();
					//client.sendConnectionRequest();
					Intent intent = new Intent(getApplication(),NetPlay.class);
//					switch (selectedPlayers) {
//					case 4:
//						intent = new Intent(getApplication(),
//								NetFourPlayerActivity.class);
//						break;
//					case 5:
//						intent = new Intent(getApplication(),
//								NetFivePlayerActivity.class);
//						break;
//					case 6:
//						intent = new Intent(getApplication(),
//								NetSixPlayerActivity.class);
//						break;
//					default:
//						intent = new Intent(getApplication(),
//								NetThreePlayerActivity.class);
//						break;
//					}
					
					intent.putExtras(gameInfo);
					startActivity(intent);
				}
			}
		});

	}
}